package org.etherframework.tests.features.command
{
	import org.etherframework.common.command.ICommands;
	import org.etherframework.core.IModule;
	import org.etherframework.dependency.IDependencies;
	import org.etherframework.tests.features.command.commands.GetItemsCommand;
	import org.etherframework.tests.features.command.commands.SampleCommand;
	import org.etherframework.tests.features.command.events.GetItemsEvent;
	import org.etherframework.tests.utils.TestUtility;
	import org.hamcrest.assertThat;
	import org.hamcrest.collection.arrayWithSize;
	import org.hamcrest.collection.hasItem;
	import org.hamcrest.object.equalTo;

	public class when_dispatching_events
	{
		private static var _testUtility:TestUtility = new TestUtility();
		private static var _module:IModule;
		private static var _g:ICommands;
		private static var _d:IDependencies;
		private static var _dispatchedEvent:GetItemsEvent;
		
		[BeforeClass]
		public static function setUpBeforeClass():void
		{
			_module = _testUtility.createModule();
			_g = _module.commands;
			_d = _module.dependencies;
			
			_d.addClass(GetItemsCommand);
			_d.addClass(SampleCommand);
			_g.map(GetItemsEvent.GET_ITEMS, GetItemsCommand);
			_g.map(GetItemsEvent.GET_ITEMS, SampleCommand);
			
			GetItemsCommand.executed = new Vector.<GetItemsCommand>();
			SampleCommand.executed = new Vector.<SampleCommand>();
			
			_dispatchedEvent = new GetItemsEvent(GetItemsEvent.GET_ITEMS);
			_module.ether.dispatch(_dispatchedEvent);
		}
		
		[AfterClass]
		public static function tearDownAfterClass():void
		{
			_testUtility.dispose();
		}
		
		[Test(order=1)]
		public function it_should_execute_commands():void
		{
			assertThat(GetItemsCommand.executed, arrayWithSize(1));
			assertThat(SampleCommand.executed, arrayWithSize(1));
		}
		
		[Test(order=2)]
		public function it_should_create_command_with_event_argument_if_defined():void
		{
			assertThat(GetItemsCommand.executed[0].event, equalTo(_dispatchedEvent));
		}
		
		[Test(order=3)]
		public function it_should_execute_only_added_commands():void
		{
			_g.unmap(GetItemsEvent.GET_ITEMS, GetItemsCommand);
			_module.ether.dispatch(new GetItemsEvent(GetItemsEvent.GET_ITEMS));
			
			assertThat(GetItemsCommand.executed, arrayWithSize(1));
			assertThat(SampleCommand.executed, arrayWithSize(2));
		}
	}
}